<template>
  <div class="header">
    <a href="/main">
      <div class="icon">
        <i class="iconfont icon-taiyang"></i>
        <p>新中地天气</p>
      </div>
    </a>

    <div class="weather" v-if="timeWeatherData">
        <h4>{{timeWeatherData.city}}</h4>
        <span>实时天气: {{timeWeatherData.weather}} {{timeWeatherData.temperature}}℃</span>
        <span>{{timeWeatherData.windDirection}}风{{timeWeatherData.windPower}}级</span>
    </div>

    <button class="warn" @click="openShowPop()">
        <span class="iconfont icon-xinxi"></span>
    </button>
    
    </div>


</template>

<script>
import {reactive,watch,ref} from 'vue'
import { useMainStore } from '../../pinia/index.js'
import AMapLoader from '@amap/amap-jsapi-loader';
export default{
emits:['openShowPop'],
setup(props,ctx){
  //实现提示信息的开
  const mainStore = useMainStore()
  const openShowPop = ()=>{
  mainStore.openShowPop()
  }
  
  //获取本地实时天气数据
  const timeWeatherData = ref({})
  const weatherData = reactive([])
  window._AMapSecurityConfig = {
  securityJsCode: '23d45164cfc2f815f5b659ab29f2b927',
  }
    AMapLoader.load({
    key: '2f13fbf16769972e3a8f7b24bbc3f8de',
    version: '2.0',
  }).then((AMap) => {
    console.log('AMap :>> ', AMap);
      // 创建天气查询实例
      AMap.plugin('AMap.Weather', () => {
      const weather = new AMap.Weather();
      // 获取实时的天气信息
      weather.getLive('武汉市', function(err, data) {
        timeWeatherData.value = data
      });
      });
    }).catch((error) => {
      console.error('Failed to load AMap:', error);
    });

     watch(weatherData, (newValue) => {
      console.log('New value:', newValue);
    });

  return{
  mainStore,
  openShowPop,
  weatherData,
  timeWeatherData,
  }
}
}
</script>

<style scoped>
.header{
  width: 100%;
  height: 80px;
  background-color: rgb(0 102 138);
  display: flex;
  align-items: center;
  box-sizing: border-box;
  padding-top: 1.5rem;
  padding-bottom: 1.5rem;
  padding-left: 8.7rem;
  gap: 1rem;
  position: relative;
}
.header::after{
  content: "";
  position: absolute;
  bottom: -5px;
  left: 0;
  width: 100%;
  height: 5px;
  background-color: rgba(0, 0, 0, 0.2);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}
.header a{
  text-decoration: inherit;
  color: white;
  height: 32px;
  margin-left: 20px;
}
.header a .icon{
  display: flex;
  align-items: center;
}
.header .icon p{
  font-size:1.5rem;
  line-height: 2rem;
  margin-left: 12px;
}
.weather{
  color:white;
  display: flex;
  height: 28px;
  align-items: center;
}
.weather h4{
  font-weight: inherit;
  font-size: 1.125rem;
  line-height: 1.75rem;
  margin-top: 5px;
}
.header .weather span{
  font-size: 0.9rem;
  margin-top: 5px;
  margin-left: 16px;
}
.header .warn{
  width: 20px;
  height: 20px;
  background: white;
  border-radius: 15px;
  text-align: center;
  color: rgb(0, 102, 138);
  margin-left: 717px;
  transition: background 1s;
  border: none;
}
.header .warn:hover{
  background: #2c4168;
}
.iconfont:hover{
  cursor: pointer;
}
</style>